﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Engine.Components.Workers
{
    public class FileMatcherInfo
    {
        public FileMatcherInfo(DataSchema.CollectionSchema.ItemRow i, FileInfoCRC fi)
        {
            Item = i;
            FileInfo = fi;
        }
        public DataSchema.CollectionSchema.ItemRow Item;
        public FileInfoCRC FileInfo;
    }
    
    public class FileMatcher:Abstract.WorkerBase
    {
        #region PRIVATE
        DataSchema.CollectionSchema _cs;
        #endregion

        #region FACTORY
        private FileMatcher(HunterPluginSDK.Interface.IProgressHandler ph, DataSchema.CollectionSchema cs)
        {
            this.ProgressHandler = ph;
            _cs = cs;
        }
        static public Engine.Components.Interface.IWorker CreateWorker(HunterPluginSDK.Interface.IProgressHandler ph, DataSchema.CollectionSchema cs)
        {
            return new FileMatcher(ph, cs);
        }
        #endregion            
        
        protected override void DoMyJob(object o)
        {
            FileInfoCRC fic = o as FileInfoCRC;
            if (fic != null)
            {
                List<DataSchema.CollectionSchema.ItemRow> l = _cs.MatchByCRC32(fic.CRC);
                if (l.Count > 0)
                {
                    foreach (DataSchema.CollectionSchema.ItemRow ir in l)
                    {
                        FileMatcherInfo fmi = new FileMatcherInfo(ir, fic);
                        this.OnProduced(fmi);
                    }
                }
            }
        }
    }
}
